require(['./config'], () => {
    require(['template', 'request', 'header', 'elevatezoom', 'footer', 'cbl', 'jquery'], (template, req, header) => {
        class Details {
            constructor() {
                this.init();
                this.count = 1;
            }
            init() {
                const id = window.location.search.split('?')[1];
                Promise.all([req.getDetailsData(id), req.getPrice(id)])
                    .then(res => {
                        this.render(res);
                    })
            }
            render(list) {
                let price = list[1].data.skuSalePriceRange[0];
                let pc = list[0].data.product.pcDetailDesc;
                let id = list[0].data.product.productId;
                const { desc, image, name, guaranteeTags,productId} = list[0].data.product;
                $('.content > .main').html(template('detailsTemplate', { desc, image, name, guaranteeTags, price ,productId}));
                $(".Boxs .pics").html(pc);
                this.initZoom();
                this.addReduce();
                this.addTocar(image,id, name, price);
            }
            initZoom() {
                $('.zoom-image').elevateZoom({
                    gallery: 'smPicBox'
                })
            }
            addReduce() {
                $(".r .sp1").click(() => {
                    this.count--;
                    if(this.count <= 1){
                        this.count = 1;
                    }
                    $(".r .num").html(this.count);
                })
                $(".r .sp2").click(() => {
                    this.count++;
                    $(".r .num").html(this.count);
                })
            }
            addTocar(image,id, name, price) {
                $(".r .btn>a:nth-child(1)").click(() => {
                    let prodList = localStorage.getItem('prodList');
                    if(prodList){
                        prodList = JSON.parse(prodList);
                        let flag = prodList.some((item,index) => {
                            return item.id === id;
                        })
                        if(flag){
                            prodList.forEach(item => {
                                item.count += item.id === id ? this.count : 0;
                            })
                        }else{
                            prodList.push({id,image,name, price,count:this.count,isChecked:true})
                        }
                        localStorage.setItem("prodList",JSON.stringify(prodList));
                    }else{
                        localStorage.setItem('prodList',JSON.stringify([{id,image, name,price,count:this.count,isChecked:true}]));
                    }
                    header.computedCount();
                })
            }
        }
        return new Details();
    })
})